package com.itheima52.mobilesafe.activity;

import com.itheima52.mobilesafe.R;
import com.itheima52.mobilesafe.utils.ToastUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 第三个设置向导页
 * 
 * @author ludaxu
 * 
 */
public class Setup3Activity extends BaseSetupActivity {
	private EditText etPhone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup3);
		etPhone = (EditText) findViewById(R.id.et_phone);
		String phone = mPref.getString("safe_phone", "");
		etPhone.setText(phone);// 如果phone为null,可能会崩溃
	}

	// 上一页
	@Override
	public void showPreviousPage() {
		startActivity(new Intent(Setup3Activity.this, Setup2Activity.class));
		finish();
		// 两个界面的切换动画
		overridePendingTransition(R.anim.tran_previous_in,
				R.anim.tran_previous_out);// 进入动画和退出动画

	}

	// 下一页
	@Override
	public void showNextPage() {

		String phone = etPhone.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			ToastUtils.showToast(Setup3Activity.this, "安全号码不能为空");
			return;
		}
		mPref.edit().putString("safe_phone", phone).commit();// 保存安全号码

		startActivity(new Intent(Setup3Activity.this, Setup4Activity.class));
		finish();
		// 两个界面的切换动画
		overridePendingTransition(R.anim.tran_in, R.anim.tran_out);// 进入动画和退出动画

	}

	/**
	 * 选择联系人
	 * 
	 * @param view
	 */
	public void selectContact(View view) {

		Intent intent = new Intent(Setup3Activity.this, ContactActivity.class);
		startActivityForResult(intent, 0);// 0为请求码

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// 当用户点击电话号码的item时,resultCode=Activity.RESULT_OK
		// 当用户点击取消时,resultCode=Activity.CANCELED
		if (resultCode == Activity.RESULT_OK) {
			String phone = data.getStringExtra("phone");
			phone = phone.replaceAll("-", "").replaceAll(" ", "");// 替换"-"和空格
			etPhone.setText(phone);// 把电话号码设置给输入框
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

}
